package com.gitee.wsl.struct.grammar.ast.process.marker.block

import com.gitee.wsl.struct.grammar.ast.process.constraints.TextReadConstraints
import com.gitee.wsl.struct.grammar.ast.process.constraints.ext.getCharsEaten
import com.gitee.wsl.struct.grammar.ast.process.marker.MarkerProcessor
import com.gitee.wsl.struct.grammar.ast.process.marker.ProductionHolder
import com.gitee.wsl.text.reader.line.LookaheadText


interface MarkerBlockProvider<T : MarkerProcessor.StateInfo> {

    fun createMarkerBlocks(pos: LookaheadText.Position,
                           productionHolder: ProductionHolder,
                           stateInfo: T): List<MarkerBlock>


    fun interruptsParagraph(pos: LookaheadText.Position, constraints: TextReadConstraints): Boolean

    
    companion object {
        fun isStartOfLineWithConstraints(pos: LookaheadText.Position, constraints: TextReadConstraints): Boolean {
            return pos.offsetInCurrentLine == constraints.getCharsEaten(pos.currentLineText)
        }
        
        fun passSmallIndent(text: CharSequence, startOffset: Int = 0): Int {
            var offset = startOffset
            repeat(3) {
                if (offset < text.length && text[offset] == ' ') {
                    offset++
                }
            }
            return offset
        }
    }
}

fun LookaheadText.Position.isStartOfLineWithConstraints(constraints: TextReadConstraints): Boolean {
    return offsetInCurrentLine == constraints.getCharsEaten(currentLineText)
}
